﻿'   
'
'   g10CMDTryItVB -               Copyright (C) 2011 PH Informática, SA.
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the  Free Software Foundation, either version 3 of the  License,  or
'   (at your option) any later version.
'
'   This  program  is distributed in  the hope that it will  be  useful,
'   but  WITHOUT  ANY  WARRANTY; without even the  implied  warranty  of
'   MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.  See  the
'   GNU General Public License for more details.
'
'   You  should have received  a copy of  the GNU General Public License
'   along with this program. If not, see <http://www.gnu.org/licenses/>.
'
'

Imports System.Text

Module Module1

    ' TokenID de utilização do serviço
    Public sApiTokenID As String = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"

    ' SessionID identificação da sessão, deve ser gerado de cada vez que uma nova sessão
    ' se inicia ou sempre que se achar necessário.
    Public sSessionID As String = System.Guid.NewGuid.ToString

    Public oAuxEntidades As g10WSTryIt.Entidades

    Dim myWS As New g10WSTryIt.g10TryItSoapClient

    Sub Main()

        Menu()

    End Sub

    Private Sub Menu()

        Dim op As String = ""

        While op <> "9"
            Console.WriteLine("g10CMDTryItVB                       PH Informática, SA")
            Console.WriteLine("                                    v. " & myWS.Version())
            Console.WriteLine("------------------------------------------------------")
            Console.WriteLine("1-Distritos")
            Console.WriteLine("2-Concelhos")
            Console.WriteLine("3-Freguesias")
            Console.WriteLine("")
            Console.WriteLine("4-Geometria")
            Console.WriteLine("")
            Console.WriteLine("9-Terminar")
            Console.WriteLine("")
            Console.Write("> ")

            op = Console.ReadLine()

            Select Case op
                Case "1" : Distritos()
                Case "2" : Concelhos()
                Case "3" : Freguesias()
                Case "4" : Geometria()
            End Select

        End While

    End Sub

    Private Sub Distritos()

        Dim ts As TimeSpan = New TimeSpan(DateTime.UtcNow.Millisecond)

        Dim oDistritos As New g10WSTryIt.Entidades
        oDistritos = myWS.getDistritos(sApiTokenID, sSessionID)

        For Each e As g10WSTryIt.Entidade In oDistritos.Entidades1
            Dim sNome As String = e.Nome
            Dim sID As String = e.Id
            Console.WriteLine(".    " & Fill(30, e.Nome, " ") & e.Id)
        Next

        Console.WriteLine("")
        Console.WriteLine(".                                                         t(" & ts.TotalMilliseconds & " ms)")
        Console.WriteLine("")
        Console.WriteLine("")

    End Sub

    Private Sub Concelhos()

        Console.Write(".    ID Distrito> ")
        Dim sDist As String = Console.ReadLine

        Dim ts As TimeSpan = New TimeSpan(DateTime.UtcNow.Millisecond)

        ' testa existência do distrito
        Dim oDist As g10WSTryIt.cDistrito = myWS.getDistrito(sApiTokenID, sSessionID, sDist)

        If oDist Is Nothing Then

            Console.WriteLine(".    Distrito não existe ou não tem acesso.")
            Console.WriteLine("")

        Else

            Dim oConcelhos As New g10WSTryIt.Entidades
            oConcelhos = myWS.getConcelhos(sApiTokenID, sSessionID, sDist)

            For Each e As g10WSTryIt.Entidade In oConcelhos.Entidades1
                Dim sNome As String = e.Nome
                Dim sID As String = e.Id
                Console.WriteLine(".    " & Fill(30, e.Nome, " ") & e.Id)
            Next

            Console.WriteLine("")
            Console.WriteLine(".                                                         t(" & ts.TotalMilliseconds & " ms)")
            Console.WriteLine("")
            Console.WriteLine("")

        End If

    End Sub

    Private Sub Freguesias()

        Console.Write(".    ID Concelho> ")
        Dim sConc As String = Console.ReadLine


        Dim ts As TimeSpan = New TimeSpan(DateTime.UtcNow.Millisecond)

        ' testa existência do distrito
        Dim oConc As g10WSTryIt.cConcelho = myWS.getConcelho(sApiTokenID, sSessionID, sConc)

        If oConc Is Nothing Then

            Console.WriteLine(".    Concelho não existe ou não tem acesso.")
            Console.WriteLine("")

        Else

            Dim oFreguesias As New g10WSTryIt.Entidades
            oFreguesias = myWS.getFreguesias(sApiTokenID, sSessionID, sConc)

            For Each e As g10WSTryIt.Entidade In oFreguesias.Entidades1
                Dim sNome As String = e.Nome
                Dim sID As String = e.Id
                Console.WriteLine(".    " & Fill(30, e.Nome, " ") & e.Id)
            Next

            Console.WriteLine("")
            Console.WriteLine(".                                                         t(" & ts.TotalMilliseconds & " ms)")
            Console.WriteLine("")
            Console.WriteLine("")

        End If

    End Sub


    Private Sub Geometria()

        Console.Write(".    ID Entidade> ")
        Dim sId As String = Console.ReadLine


        Dim ts As TimeSpan = New TimeSpan(DateTime.UtcNow.Millisecond)

        ' testa existência do distrito
        Dim oGeo As g10WSTryIt.g10GeoCollection = myWS.getGeometryById(sApiTokenID, sSessionID, sId)

        If oGeo Is Nothing Then

            Console.WriteLine(".    Entidade não existe ou não tem acesso.")
            Console.WriteLine("")

        Else

            Console.WriteLine(oGeo.WKT)

            Console.WriteLine("")
            Console.WriteLine(".                                                         t(" & ts.TotalMilliseconds & " ms)")
            Console.WriteLine("")
            Console.WriteLine("")

        End If

    End Sub

    ' utilidades

    Private Function Fill(ByVal total_length As Integer, ByVal str As String, ByVal [with] As Char) As String
        If total_length > str.Length Then
            Return str & Replicate(total_length - str.Length, [with])

        Else
            Return str
        End If
    End Function

    Private Function Replicate(ByVal k As Integer, ByVal what As Char) As String

        Dim sRet As New StringBuilder(what)

        If k = 1 Then
            Return what
        Else
            Return sRet.Append(Replicate(k - 1, what)).ToString
        End If

    End Function

End Module
